/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2024-10-28
 * Time: 19:51
 */
public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode root = null;
    public void insert(int key) {
        TreeNode node = new TreeNode(key);
        if(root == null) {
            root = node;
            return;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        while(cur != null) {
            if (cur.val == key) {
                return;
            } else if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else {
                parent = cur;
                cur = cur.right;
            }
        }
        if(parent.val > key) {
            parent.left = node;
        }else {
            parent.right = node;
        }
    }

    /**
     * 时间复杂度:
     *          最好情况:O(logN)
     *          最坏情况:O(N)
     * @param key
     * @return
     */
    public TreeNode search(int key) {
        TreeNode cur = root;
        while(cur != null) {
            if(cur.val == key) {
                return cur;
            }else if(cur.val > key) {
                cur = cur.left;
            }else  {
                cur = cur.right;
            }
        }
        return null;
    }
    public void remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null) {
            if(key < cur.val) {
                parent = cur;
                cur = cur.left;
            }else if(key > cur.val) {
                parent = cur;
                cur = cur.right;
            }else {
                removeNode(parent,cur);
                return;
            }
        }
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null) {
            if(cur == root) {
                root = root.right;
            }else if(cur == parent.left) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if(cur == root) {
                root = root.left;
            }else if(cur == parent.left) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            //第一种：找左边最大值
            /*TreeNode target = cur.left;
            TreeNode targetParent = cur;
            while(target.right != null) {
                targetParent = target;
                target = target.right;
            }
            cur.val = target.val;
            if(targetParent.left == target) {
                targetParent.right = target.left;
            }else {
                targetParent.left = target.left;
            }*/
            //第二种：找右边最小
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while(target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(targetParent.left == target) {
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }
        }
    }
}
